      SUBROUTINE INVERT (A, NZ, MAXZ, INDEX, ROW, KOL)
C
      IMPLICIT REAL*8 (A-H,O-Z)
C     THIS SUBROUTINE WILL INVERT A MATRIX OF ANY ORDER (LIMITED BY
C       COMPUTER CAPACITY) BY A MODIFIED GAUSS-ELIMINATION METHOD.
C         A = THE INPUT MATRIX AS WELL AS THE OUTPUT MATRIX.
C        NZ = THE ORDER OF MATRIX A.
C      MAXZ = THE MAXIMUM ORDER DIMENSIONED IN THE CALLING PROGRAM.
C             THIS PERMITS THE USER TO INVERT ANY MATRIX FROM A 2 BY 2
C               UP TO A MAXZ BY MAXZ FROM CASE TO CASE.
C     INDEX = 1 IF THE INVERSE IS FOUND.
C           = 2 IF THE INPUT MATRIX IS SINGULAR.
C           = 3 IF MACHINE ERROR OCCURRED. IF PROGRAMMER WISHES TO LOOP
C               BACK FOR ANOTHER TRY, BE SURE TO RESET THE INPUT MATRIX.
C       ROW = TEMPORARY WORK STORAGE.
C       KOL = TEMPORARY WORK STORAGE.
C     MATRIX A IS DUMMY DIMENSIONED. THIS SUBROUTINE REFERS TO IT AS A
C       SINGLE DIMENSIONED VARIABLE BY FINDING THE PROPER SUBSCRIPT.
      DIMENSION  A(2), ROW(2), KOL(2)
      N=NZ
      MAX=MAXZ
      KOL(1)=1
      DO 10 I=2,N
      KOL(I)=KOL(I-1)+1
   10 CONTINUE
      DO 120 K=1,N
      L=N-K+1
      M=KOL(1)
      J=1
      IF(N-K) 190,60,20
   20 AMPY=DABS(A(1))
      DO 40 I=2,L
      IF(AMPY-DABS(A(I))) 30,40,40
   30 J=I
      AMPY=DABS(A(I))
      M=KOL(I)
   40 CONTINUE
      IF(KOL(1)-M) 50,60,50
   50 KOL(J)=KOL(1)
      KOL(1)=M
   60 IF(A(J)) 70,200,70
   70 AMPY=A(J)
      DO 80 I=2,N
      IS=(I-1)*MAX+J
      ROW(I-1)=A(IS)/AMPY
      IC=(I-2)*MAX
      IS=IC+J
      IT=IC+1
      A(IS)=A(IT)
   80 CONTINUE
      ROW(N)=1.0D0/AMPY
      IC=(N-1)*MAX
      IS=IC+J
      IT=IC+1
      A(IS)=A(IT)
      DO 100 I=2,N
      AMPY=A(I)
      DO 90 J=2,N
      IS=(J-2)*MAX+I-1
      IT=(J-1)*MAX+I
      A(IS)=A(IT)-AMPY*ROW(J-1)
   90 CONTINUE
      IS=(N-1)*MAX+I-1
      A(IS)= -AMPY*ROW(N)
  100 CONTINUE
      DO 110 J=1,N
      KOL(J)=KOL(J+1)
      IS=(J-1)*MAX+N
      A(IS)=ROW(J)
  110 CONTINUE
      KOL(N)=M
  120 CONTINUE
      DO 170 K=1,N
      IF(KOL(K)-K) 190,170,130
  130 DO 160 I=K,N
      IF(KOL(I)-K) 190,140,160
  140 DO 150 J=1,N
      IS=(I-1)*MAX+J
      IT=(K-1)*MAX+J
      ROW(1)=A(IS)
      A(IS)=A(IT)
      A(IT)=ROW(1)
  150 CONTINUE
      M=KOL(K)
      KOL(K)=KOL(I)
      KOL(I)=M
      GO TO 170
  160 CONTINUE
      INDEX=3
      GO TO 180
  170 CONTINUE
      INDEX=1
  180 RETURN
  190 INDEX=3
      GO TO 180
  200 INDEX=2
      GO TO 180
      END
